﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.Summary
{
    /// <summary>
    /// 获取任务的概要信息
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("Summary", "GetDataTableList", "1.0")]
    public class GetDataTableListAction : BaseAction<GetDataTableListParams, GetDataTableListResult>
    {
        protected override ActionResult<GetDataTableListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetDataTableListParams opParams)
        {
            SER.QueryService service = new SER.QueryService();
            SingleResult<CPPEI.ETL.Domain.Data.DtTableSummary> result = service.Query4DtTable();
            if(result.IsSuccess)
            {
                GetDataTableListResult actResult = new GetDataTableListResult();
                actResult.TotalCount = result.Data.TotalCount;
                actResult.RawTableCount = result.Data.RawTableCount;
                actResult.NoneRawTableCount = result.Data.NoneRawTableCount;

                for (int index = 0; index < result.Data.Tables.Count; index++)
                {
                    GetDataTableListResult.Item item = new GetDataTableListResult.Item();
                    item.Id = result.Data.Tables[index].Id;
                    item.Name = result.Data.Tables[index].Name;
                    item.CreatedTime = result.Data.Tables[index].CreatedTime;
                    item.IsRawDt = result.Data.Tables[index].IsRawDt;

                    Domain.Data.DtTableSummary.Table table = result.Data.Tables[index];

                    item.Sources = new List<GetDataTableListResult.Item.Source>();
                    for (int sourceIndex = 0; sourceIndex < table.Sources.Count; sourceIndex++)
                    {
                        GetDataTableListResult.Item.Source source = new GetDataTableListResult.Item.Source();
                        source.DataSourceId = table.Sources[sourceIndex].Id;
                        source.DataSourceName = table.Sources[sourceIndex].Name;
                        source.DataServiceId = table.Sources[sourceIndex].DtSerId;
                        item.Sources.Add(source);
                    }

                    actResult.Tables.Add(item);
                }

                return new ActionResult<GetDataTableListResult>(actResult);
            }

            return new ActionResult<GetDataTableListResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
        }
    }

    public class GetDataTableListParams
    {
    }

    public class GetDataTableListResult
    {
        public int TotalCount { get; set; }
        public int RawTableCount { get; set; }
        public int NoneRawTableCount { get; set; }

        /// <summary>
        /// 最近执行的15条任务
        /// </summary>
        public List<Item> Tables = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public DateTime CreatedTime { get; set; }
            public bool IsRawDt { get; set; }

            public List<Source> Sources { get; set; }

            public class Source
            {
                public Guid DataSourceId { get; set; }

                public string DataSourceName { get; set; }

                public Guid DataServiceId { get; set; }
            }
        }
    }
}
